package com.zhiyume.service;

import com.zhiyume.dao.api.TAdminMapper;
import com.zhiyume.dao.api.TAdminRoleMapper;
import com.zhiyume.dao.api.TRoleMapper;
import com.zhiyume.dao.entity.*;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.EncryptUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.vo.UserReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiaxz
 * @date 2017/12/24 15:20
 * @description
 */
@Service
public class UserManagerService {
    @Autowired
    private TAdminMapper tAdminMapper;
    @Autowired
    private TRoleMapper tRoleMapper;
    @Autowired
    private TAdminRoleMapper tAdminRoleMapper;
    @Value("${custom.password}")
    private String defaultPwd;

    /**
     * 查询所有的用户信息
     * @return
     */
    public List<TAdmin> queryAllUserInfoList(UserReqVO dto){
        TAdminCriteria example = new TAdminCriteria();
        TAdminCriteria.Criteria c = example.createCriteria();
        example.setLimitStart(dto.getPage()*dto.getPageSize());
        example.setLimitEnd(dto.getPageSize());
        if(StringUtil.isNotBlank(dto.getUserName())) {
            c.andUserNameLike("%"+dto.getUserName()+"%");
        }
        return tAdminMapper.selectByExample(example);
    }
    public int queryCount(UserReqVO dto){
        TAdminCriteria example = new TAdminCriteria();
        TAdminCriteria.Criteria c = example.createCriteria();
        example.setLimitStart(dto.getPage()*dto.getPageSize());
        example.setLimitEnd(dto.getPageSize());
        if(StringUtil.isNotBlank(dto.getUserName())) {
            c.andUserNameLike("%"+dto.getUserName()+"%");
        }
        return tAdminMapper.countByExample(example);
    }

    /**
     * 根据Id查询管理员信息
     * @param id
     * @return
     */
    public TAdmin queryUserInfoById(long id) {
        TAdminCriteria example = new TAdminCriteria();
        TAdminCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TAdmin> list =  tAdminMapper.selectByExample(example);
        if(null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据Id修改管理员信息
     * @param tAdmin
     * @return
     */
    public int updateUserById(TAdmin tAdmin) {
        TAdminCriteria example = new TAdminCriteria();
        TAdminCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(tAdmin.getId());
        tAdmin.setUpdateTime(DateUtil.getNow());
        return tAdminMapper.updateByExampleSelective(tAdmin,example);
    }
    /**
     * 根据Id删除管理员信息
     * @param id
     * @return
     */
    public Map deleteUserById(long id) {
        TAdminCriteria example = new TAdminCriteria();
        TAdminCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        Map<String,String> map = new HashMap<>();
        map.put("result","fail");
        List<TAdmin> list = tAdminMapper.selectByExample(example);
        if(null != list && list.size() > 0) {
            TAdmin tAdmin = list.get(0);
            tAdmin.setStatus(4);
            tAdmin.setUpdateTime(DateUtil.getNow());
            int i = tAdminMapper.updateByExample(tAdmin,example);
            if(i>0) {
                map.put("result","success");
            }

        }
        return map;
    }
    public Map addUserBy(TAdmin tAdmin , int roleId) {
        Map<String,String> map = new HashMap<>();
        TAdminCriteria example = new TAdminCriteria();
        TAdminCriteria.Criteria c = example.createCriteria();
        c.andUserNameEqualTo(tAdmin.getUserName());
        c.andStatusNotEqualTo(4);
        List<TAdmin> list = tAdminMapper.selectByExample(example);
        if(null != list && list.size() >0) {
            map.put("result","registed");
        }
        long id = GenerateKeyUtil.generationPK();
        tAdmin.setId(id);
        tAdmin.setPassword(EncryptUtil.SHA256(defaultPwd));
        tAdmin.setCreateTime(DateUtil.getNow());
        tAdminMapper.insertSelective(tAdmin);
        TAdminRole tAdminRole = new TAdminRole();
        tAdminRole.setRoleId(roleId);
        tAdminRole.setCreateTime(DateUtil.getNow());
        tAdminRole.setAdminId(String.valueOf(id));
        tAdminRoleMapper.insertSelective(tAdminRole);
        map.put("result","success");
        return map;
    }
    public List<TRole> queryAllRole(){
        TRoleCriteria example = new TRoleCriteria();
        TRoleCriteria.Criteria c = example.createCriteria();
        c.andStatueEqualTo("0");
        return tRoleMapper.selectByExample(example);
    }
}
